<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form">
      <el-form-item label="菜单">
        <div class="vab-tree-border">
          <el-tree
            ref="treeRef"
            :data="MenusList"
            :default-checked-keys="ids"
            :default-expand-all="true"
            :check-strictly="true"
            node-key="value"
            show-checkbox
          >
            <template #default="{ data }">
              <span>{{ data.label }}</span>
            </template>
          </el-tree>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { editRoleroleMenuAllot } from '@/api/roleManagement'
  import { getList } from '@/api/router'

  export default defineComponent({
    name: 'RoleManagementEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        MenusList: [],
        ids: [],
        formRef: null,
        treeRef: null,
        form: {
          btnRolesCheckedList: [],
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
        list: [],
      })

      const showEdit = (row, list, ids) => {
        state.MenusList = list
        state.title = '权限分配'
        if (!row) {
          console.log()
        } else {
          state.form = JSON.parse(JSON.stringify(row))
          state.ids = ids
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          btnRolesCheckedList: [],
        }
        state.ids = []
        state.MenusList = []
        state.dialogFormVisible = false
      }
      const fetchData = async () => {
        const { data: data } = await getList()
        state.list = data
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const tree = state['treeRef'].getCheckedKeys()
            // console.log(tree, 'sjhdsjhji')
            try {
              const { msg } = await editRoleroleMenuAllot({
                roleId: state.form.id,
                tree: tree,
              })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              emit('fetch-data')
              close()
            } catch (e) {
              console.log(e)
            }
          }
        })
      }
      onMounted(() => {
        fetchData()
      })
      return {
        ...toRefs(state),
        showEdit,
        close,
        fetchData,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    width: 100%;
    height: auto;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
